package com.sugon.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.sugon.dao.ModuleSpDeptStockDao;
import com.sugon.dao.ModuleSpStockDao;
import com.sugon.entity.ModuleSpDemandItemEntity;
import com.sugon.entity.ModuleSpDeptStockEntity;
import com.sugon.entity.ModuleSpStockEntity;
import com.sugon.entity.StoreHouseEntity;
import com.sugon.modules.mold.consts.MoldSpStockState;
import com.sugon.service.ModuleSpDemandItemService;
import com.sugon.service.ModuleSpDeptStockService;
import com.sugon.service.StoreHouseService;
import com.sugon.utils.Constant;
import com.sugon.utils.ShiroUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 车间型芯库存状态统计Service实现类
 *
 * @author sugon
 * @date 2022-07-07 17:05:34
 */
@Service("moduleSpDeptStockService")
public class ModuleSpDeptStockServiceImpl implements ModuleSpDeptStockService {

    @Resource
    private ModuleSpDeptStockDao moduleSpDeptStockDao;
    @Resource
    private ModuleSpStockDao moldSpStockDao;
    @Resource
    private StoreHouseService storeHouseService;
    @Resource
    private ModuleSpDemandItemService moldSpDemandItemService;

    @Override
    public void realtimeUpdateDeptStock(String deptId, String moldSpId) {
        ModuleSpDeptStockEntity queryParam = new ModuleSpDeptStockEntity(deptId, moldSpId);
        List<ModuleSpDeptStockEntity> deptMoldSpList = queryList(BeanUtil.beanToMap(queryParam));
        // 如果为空，则说明系统并未建立库存信息，需要新增并实时更新库存
        if (CollectionUtil.isEmpty(deptMoldSpList)) {
            // 设置车间信息
            StoreHouseEntity storeHouse = storeHouseService.queryByWorkshop(3, deptId);
            if (storeHouse != null) {
                queryParam.setStoreId(storeHouse.getId());
            }
            save(queryParam);
            deptMoldSpList = CollectionUtil.newArrayList(queryParam);
        }

        // 声明库存数、入库数、出库数、在途数
        long stockNum, stockInNum, stockOutNum, transitNum;
        for (ModuleSpDeptStockEntity deptMoldSp : deptMoldSpList) {
            // 初始化为 0
            stockNum = 0;
            stockInNum = 0;
            stockOutNum = 0;
            transitNum = 0;
            // 计算采购数
            // 在途数： 直接从型芯计划明细中获取
            List<ModuleSpDemandItemEntity> demandItems = moldSpDemandItemService.queryByMoldSpId(deptMoldSp.getMoldSpId());
            if (CollectionUtil.isNotEmpty(demandItems)) {
                // 需求数
                transitNum = demandItems.stream().filter(x -> x.getTransitNum() != null).collect(Collectors.summingInt(x -> x.getTransitNum()));
            }
            // 计算库存数、入库数、出库数
            List<ModuleSpStockEntity> spStockList = moldSpStockDao.queryMoldSpStock(deptMoldSp.getStoreId(), deptMoldSp.getMoldSpId());
            if (CollectionUtil.isNotEmpty(spStockList)) {
                // 入库数
                stockInNum = spStockList.size();
                // 出库数
                stockOutNum = spStockList.stream().filter(spStock -> CollectionUtil.newArrayList(MoldSpStockState.LOCKING, MoldSpStockState.USED).contains(spStock.getState())).count();
                // 库存数
                stockNum = stockInNum - stockOutNum;
            }
            deptMoldSp.setStockNum(stockNum);
            deptMoldSp.setStockInNum(stockInNum);
            deptMoldSp.setStockOutNum(stockOutNum);
            deptMoldSp.setTransitNum(transitNum);
            update(deptMoldSp);
        }
    }

    @Override
    public ModuleSpDeptStockEntity queryObject(String id) {
        return moduleSpDeptStockDao.queryObject(id);
    }

    @Override
    public List<ModuleSpDeptStockEntity> queryList(Map<String, Object> map) {
        return moduleSpDeptStockDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return moduleSpDeptStockDao.queryTotal(map);
    }

    @Override
    public int save(ModuleSpDeptStockEntity moduleSpDeptStock) {
        moduleSpDeptStock.setId(RandomUtil.randomString(32));
        moduleSpDeptStock.setDelFlag(Constant.IS_NOT_DEL_INT);
        moduleSpDeptStock.setGmtCreate(DateUtil.date());
        moduleSpDeptStock.setCreateUser(ShiroUtils.getUserId());
        return moduleSpDeptStockDao.save(moduleSpDeptStock);
    }

    @Override
    public int update(ModuleSpDeptStockEntity moduleSpDeptStock) {
        moduleSpDeptStock.setGmtModified(DateUtil.date());
        moduleSpDeptStock.setModifyUser(ShiroUtils.getUserId());
        return moduleSpDeptStockDao.update(moduleSpDeptStock);
    }

    @Override
    public int delete(String id) {
        return moduleSpDeptStockDao.delete(id);
    }

    @Override
    public int deleteBatch(String[] ids) {
        return moduleSpDeptStockDao.deleteBatch(ids);
    }
}
